package com.vince.xq.dataCompare.service;

import com.vince.xq.dataCompare.common.Constant;
import com.vince.xq.dataCompare.model.DbConfigEbo;
import com.vince.xq.dataCompare.model.JobConfigEbo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author xiaoqiu2017wy@163.com
 * @Date 2022/8/6
 */
@Service
public class InstanceService {

    private static final Logger log = LoggerFactory.getLogger(InstanceService.class);

    private static String countCompareSql = "SELECT  base.uv,\n" +
            "         verify.uv AS verify_uv,\n" +
            "         base.uv - verify.uv AS diff_uv \n" +
            "from\n" +
            "    (SELECT 'total_num' AS name,  count(distinct [origin_primary_key]) AS uv\n" +
            "    FROM [origin_table] ) base left outer join\n" +
            "    (SELECT 'total_num' AS name, count(distinct [to_primary_key]) AS uv\n" +
            "    FROM [to_table] ) verify\n" +
            "ON base.name = verify.name\n" +
            "\n";

    @Autowired
    private JobService jobService;

    @Autowired
    private DbService dbService;

    public void run(Long job_config_id) throws Exception {
        JobConfigEbo jobConfigEbo = jobService.getDbConfigEboById(job_config_id);
        DbConfigEbo dbConfigEbo = dbService.getDbConfigEboById(jobConfigEbo.getDbConfigId());
        runAndInert(jobConfigEbo, dbConfigEbo);
    }

    public void runAndInert(JobConfigEbo jobConfigEbo, DbConfigEbo dbConfigEbo) throws Exception {
        String originTablePrimaryMd5 = DigestUtils.md5DigestAsHex((jobConfigEbo.getOriginTablePrimary()).getBytes());
        String toTablePrimaryMd5 = DigestUtils.md5DigestAsHex((jobConfigEbo.getToTablePrimary()).getBytes());
        String originTable = jobConfigEbo.getOriginTableName();
        String toTable = jobConfigEbo.getToTableName();
        countCompareSql = countCompareSql.replace("[origin_primary_key]", originTablePrimaryMd5)
                .replace("[to_primary_key]", toTablePrimaryMd5).replace("[origin_table]", originTable)
                .replace("[to_table]", toTable);
        String connectDriver;
        switch (dbConfigEbo.getType()) {
            case Constant.hiveType:
                connectDriver = Constant.hiveConnectDriver;
                break;
            case Constant.mysqlType:
                connectDriver = Constant.mysqlConnectDriver;
                break;
            default:
                connectDriver = "";
        }
        runCountCompare(countCompareSql, dbConfigEbo, connectDriver);
    }


    private void runCountCompare(String sql, DbConfigEbo dbConfigEbo, String connectDriver) throws Exception {
        try {
            Class.forName(connectDriver);
        } catch (ClassNotFoundException e) {
            throw new Exception("注册驱动失败");
        }
        Connection conn = null;
        try {
            String url = dbConfigEbo.getUrl();
            conn = DriverManager.getConnection(url, dbConfigEbo.getUser(), dbConfigEbo.getPwd());
            Statement stat = conn.createStatement();
            ResultSet re = stat.executeQuery(sql);
            int i = 0;
            String baseUv;
            String verifyUv;
            String diffUv;
            while (re.next()) {
                baseUv = re.getString(1);
                verifyUv = re.getString(2);
                diffUv = re.getString(3);
            }
            re.close();
            stat.close();
            conn.close();
        } catch (SQLException e) {
            throw new Exception("数据库连接失败");
        }
    }

}
